﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CarBloke
{
    public class UnitConversions
    {
        private static double _gallonsPerLiter = 0.264172052;        
        private static double _litersPerGallon = 3.78541178;

        private static double _imperialGallonsPerLiter = 0.219969157;
        private static double _litersPerImperialGallon = 4.54609188;

        private static double _gallonsPerImperialGallon = 1.20095042;
        private static double _imperialGallonsPerGallon = 0.83267384;

        private static double _milesPerKilometer = 0.621371192;
        private static double _kilometersPerMile = 1.609344;

        public static Double Efficiency(Double kilometersPerLiter, ApplicationSettings.EfficiencyUnits efficiencyUnits)
        {
            if (Double.IsNaN(kilometersPerLiter) || Double.IsInfinity(kilometersPerLiter))
            {
                return kilometersPerLiter;
            }

            double result = Double.NaN;
            switch (efficiencyUnits)
            {
                case ApplicationSettings.EfficiencyUnits.MilesPerGallon:
                    result = kilometersPerLiter * _litersPerGallon * _milesPerKilometer;
                    break;
                case ApplicationSettings.EfficiencyUnits.MilesPerImperialGallon:
                    result = kilometersPerLiter * _litersPerImperialGallon * _milesPerKilometer;
                    break;
                case ApplicationSettings.EfficiencyUnits.KilometersPerLiter:
                    result = kilometersPerLiter;
                    break;
                case ApplicationSettings.EfficiencyUnits.LiterPer100Kilometers:
                    result = kilometersPerLiter == 0 ? 0 : 100 / kilometersPerLiter;
                    break;
            }

            return result;
        }

        public static Double Convert(Double value, ApplicationSettings.VolumeUnits from, ApplicationSettings.VolumeUnits to)
        {
            if (Double.IsNaN(value) || Double.IsInfinity(value))
            {
                return value;
            }

            Double result = Double.NaN;
            switch (from)
            {
                case ApplicationSettings.VolumeUnits.Gallon:
                    double gallons = value;
                    switch (to)
                    {                            
                        case ApplicationSettings.VolumeUnits.Gallon:
                            result = gallons;
                            break;
                        case ApplicationSettings.VolumeUnits.ImperialGallon:
                            result = gallons * _imperialGallonsPerGallon;
                            break;
                        case ApplicationSettings.VolumeUnits.Liter:
                            result = gallons * _litersPerGallon;
                            break;
                    }
                    break;
                case ApplicationSettings.VolumeUnits.ImperialGallon:
                    double imperialGallons = value;
                    switch (to)
                    {
                        case ApplicationSettings.VolumeUnits.Gallon:
                            result = imperialGallons * _gallonsPerImperialGallon;
                            break;
                        case ApplicationSettings.VolumeUnits.ImperialGallon:
                            result = imperialGallons;
                            break;
                        case ApplicationSettings.VolumeUnits.Liter:
                            result = imperialGallons * _litersPerImperialGallon;
                            break;
                    }
                    break;
                case ApplicationSettings.VolumeUnits.Liter:
                    double liters = value;
                    switch (to)
                    {
                        case ApplicationSettings.VolumeUnits.Gallon:
                            result = liters * _gallonsPerLiter;
                            break;
                        case ApplicationSettings.VolumeUnits.ImperialGallon:
                            result = liters * _imperialGallonsPerLiter;
                            break;
                        case ApplicationSettings.VolumeUnits.Liter:
                            result = liters;
                            break;
                    }
                    break;
            }

            return result;
        }

        public static Double Convert(Double value, ApplicationSettings.DistanceUnits from, ApplicationSettings.DistanceUnits to)
        {
            if (Double.IsNaN(value) || Double.IsInfinity(value))
            {
                return value;
            }

            Double result = Double.NaN;
            switch (from)
            {
                case ApplicationSettings.DistanceUnits.Kilometers:
                    double kilometers = value;
                    switch (to)
                    {
                        case ApplicationSettings.DistanceUnits.Kilometers:
                            result = kilometers;
                            break;
                        case ApplicationSettings.DistanceUnits.Miles:
                            result = kilometers * _milesPerKilometer;
                            break;
                    }
                    break;
                case ApplicationSettings.DistanceUnits.Miles:
                    double miles = value;
                    switch (to)
                    {
                        case ApplicationSettings.DistanceUnits.Kilometers:
                            result = miles * _kilometersPerMile;
                            break;
                        case ApplicationSettings.DistanceUnits.Miles:
                            result = miles;
                            break;
                    }
                    break;
            }

            return result;
        }
    }
}
